package adebo.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import adebo.utils.DAOProperties;

public abstract class DAOFactory {

	private static final String PROPERTY_URL = "url";
	private static final String PROPERTY_DRIVER = "driver";
	private static final String PROPERTY_USERNAME = "username";
	private static final String PROPERTY_PASSWORD = "password";
	private static final String PROPERTY_DB_NAME = "dbname";

	public abstract DataSource setupDataSource();
	
	public static DAOFactory getInstance(String name) {
		if (name == null) {
			// throw new DAOConfigurationException("Database name is null.");
			// TODO
		}

		DAOProperties properties = new DAOProperties(name);
		String url = properties.getProperty(PROPERTY_URL);
		String driverClassName = properties.getProperty(PROPERTY_DRIVER);
		String password = properties.getProperty(PROPERTY_PASSWORD);
		String username = properties.getProperty(PROPERTY_USERNAME);
		String dbname = properties.getProperty(PROPERTY_DB_NAME);

		DAOFactory instance = null;

		// If driver is specified, then load it to let it register itself with DriverManager.
//		if (driverClassName != null) {
//			try {
//				Class.forName(driverClassName);
//			} catch (ClassNotFoundException e) {
//				// throw new DAOConfigurationException("Driver class '" +
//				// driverClassName + "' is missing in classpath.", e);
//				// TODO Exception Behandlung
//			}
//		}
		instance = new DriverManagerDAOFactory(url, username, password,dbname, driverClassName);

		return instance;
	}
	
	public abstract Connection getConnection() throws SQLException;
}
